import { Init, Provide } from '@midwayjs/core';
import { StudentEntity } from '../entity/student';
import { BaseService } from '@cool-midway/core';
import { ProcedureBaseService } from '../../../comm/service/procedure.base.service';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Equal, In, Repository } from 'typeorm';

/**
 * 学生信息服务
 */
@Provide()
export class StudentInfoService extends ProcedureBaseService {
  @InjectEntityModel(StudentEntity)
  studentEntity: Repository<StudentEntity>;
  constructor() {
    super();
    this.entity = StudentEntity;
    this.procedurePrefix = 'sp_student_';
    this.enableProcedure = true;
  }

  @Init()
  async init() {
    await super.init();
    this.setEntity(this.entity);
  }

  async list(query = {}) {
    if (this.enableProcedure) {
      const procedureName = `${this.procedurePrefix}list`;
      const entityMetadata = this.dataSource.getMetadata(this.entity);
      const columns = entityMetadata.columns
        .filter(col => !col.isVirtual)
        .map(col => col.databaseName);
      
      // 为每个列设置默认值为 null
      const params = columns.map(() => null);
      const sql = `CALL ${procedureName}(${params.map(() => '?').join(',')})`;
      const results = await this.dataSource.query(sql, params);
      return results[0] || [];
    }
    return super.list(query);
  }
}
